/**
 * Description: 登录
 *
 * @author      Eden
 * @datetime    2016/11/25 09:30
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

(function () {
    "use strict";

    var api = {
        getCashoutLists: '/money/cashout/getCashoutLists',
        passUrl: '/money/cashout/pass',
        rejectUrl: '/money/cashout/reject'
    };
    sm.controller('admin-cashout-list-controller', ['$scope', '$http', 'Modal', function ($scope, $http, Modal) {

        $scope.sources = {
            wx: '微信',
            app: '客户端'
        };

        $scope.nowType = 'all';
        $scope.page = 1;
        $scope.nextPage = false;
        $scope.rejectRemark = '';
        $scope.cashoutTotal = 0;
        $scope.cashoutLists = [];

        $scope.menus = [{
            'text': '全部申请',
            'alias': 'all'
        }, {
            'text': '未处理',
            'alias': 'pending'
        }, {
            'text': '已通过',
            'alias': 'pass'
        }, {
            'text': '已驳回',
            'alias': 'reject'
        }, {
            'text': '审核失败',
            'alias': 'failed'
        }];


        $scope.getCashout = function () {
            $http.get(api.getCashoutLists, {
                params: {
                    status: $scope.nowType,
                    page: $scope.page
                }
            }).success(function (res) {
                if (res.state) {
                    $scope.nextPage  =res.data.nextPage;
                    $scope.cashoutTotal = res.data.total;
                    appendData($scope.cashoutLists, res.data.data);
                }
            });
        };

        function appendData(oldLists, newLists) {
            if (newLists.length == 0) {
                return false;
            }
            angular.forEach(newLists, function (val) {
                oldLists.push(val);
            });
        }

        $scope.getCashout();

        // 通过
        $scope.pass = function (index) {
            if (!$scope.cashoutLists[index].id) {
                return false;
            }

            Modal.alert({
                title: '操作提示',
                content: "您确定要通过该用户提现申请吗?",
                buttons: [{
                    name: '取消',
                    type: 'cancel'
                }, {
                    name: '通过申请',
                    event: function () {
                        doPass(index)
                    }
                }]
            });
        };

        function doPass(index)
        {
            $http.post(api.passUrl, {
                id: $scope.cashoutLists[index]['id']
            }).success(function (res) {
                if (res.state) {
                    angular.extend($scope.cashoutLists[index], res.data);
                    if (res.data.status == 5) {
                        Modal.error(res.data.error);
                    }
                } else {
                    Modal.error({
                        title: "结果提醒",
                        content: res.error ? res.error : '审核失败'
                    });
                }
            });
        }

        // 驳回
        $scope.reject = function (index) {
            if (!$scope.cashoutLists[index].id) {
                return false;
            }

            $scope.selectRejectIndex = index;
            $('#testModal').modal('show');
        };

        /**
         * 拒绝
         * @param index
         */
        $scope.doReject = function (index) {
            $http.post(api.rejectUrl, {
                id: $scope.cashoutLists[index]['id'],
                remark: $scope.rejectRemark
            }).success(function (res) {
                $('#testModal').modal('hide');
                if (res.state) {
                    angular.extend($scope.cashoutLists[index], res.data);
                } else {
                    Modal.error({
                        title: "结果提醒",
                        content: res.error ? res.error : '操作失败'
                    });
                }
            });
        };

        $scope.select = function (type) {
            if ($scope.nowType == type) {
                return false;
            }

            $scope.cashoutLists = [];
            $scope.nextPage = false;
            $scope.page = 1;

            $scope.nowType = type;
            $scope.getCashout();
        };

        $scope.loadMore = function () {
            if (!$scope.nextPage) {
                return false;
            }

            $scope.page += 1;
            $scope.getCashout();
        };

    }]);

})();

